package com.rrx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rrx.model.Administrator;
import com.rrx.service.AdministratorService;
import com.rrx.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class AdministratorController {
    @Resource
    AdministratorService administratorService;
    @Resource
    RoleService roleService;

    @RequestMapping("/adminLogin")
    @ResponseBody
    public String adminLogin(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username != null && password != null && username.trim() != "" && password.trim() != "") {
            Administrator admin = administratorService.login(username, password);
            if(admin != null) {
                request.getSession().setAttribute("loginAdmin", admin);
                return "success";
            }
        }
        return "fail";
    }

    @RequestMapping("/logout")
    public String adminLogout(HttpServletRequest request) {
        Administrator admin = (Administrator) request.getSession().getAttribute("loginAdmin");
        if(admin != null)
            request.getSession().removeAttribute("loginAdmin");
        return "login";
    }

    @RequestMapping("/addAdmin")
    @ResponseBody
    public String addAdmin(HttpServletRequest request) {
        String username = request.getParameter("admin_username");
        if(!isUsername(username))
            return "usernameError";
        String password = request.getParameter("admin_password");
        if(!isPassword("password"))
            return "passwordError";
        String email = request.getParameter("admin_email");
        if(!isEmail(email))
            return "emailError";
        String[] requestRoleIds = request.getParameterValues("role_checkbox");
        if(username != null && password != null && email != null  && username.trim() != "" && password.trim() != "" && email.trim() != "") {
            administratorService.addAdmin(username, password, email, requestRoleIds);
            return "success";

        }
        return "error";
    }

    @RequestMapping("/updateAdmin")
    @ResponseBody
    public String updateAdmin(HttpServletRequest request) {
        String userid = request.getParameter("admin_id");
        String username = request.getParameter("admin_username");
        String[] roleIds = request.getParameterValues("role_checkbox");
        if(!isUsername(username))
            return "usernameError";
        String password = request.getParameter("admin_password");
        if(!isPassword(password))
            return "passwordError";
        String email = request.getParameter("admin_email");
        if(!isEmail(email))
            return "emailError";
        String userState = request.getParameter("admin_state");
        int id = -1;
        int state = -1;
        try {
            id = Integer.parseInt(userid);
            state = Integer.parseInt(userState);
        }catch(Exception e) {}
        if(administratorService.updateAdmin(username, id, password, email, state)) {
            administratorService.deleteAllAdminRoles(id, roleIds);
            return "success";
        }
        return "error";
    }

    public String updateAdminRoles(int adminId, int[] roleIds) {
        return "";
    }

    @RequestMapping("/getAdmins")
    @ResponseBody
    public String getAdmins(HttpServletRequest request) {
        String requestPage = request.getParameter("page");
        int page = 0;
        try {
            page = (Integer.parseInt(requestPage) - 1) * 10;
        }catch(Exception e) {}
        Administrator[] admins = administratorService.getAdminsLimited(page);
        int adminCount = administratorService.getAdminCount();
        int length = (int) Math.ceil((adminCount / 10.0));
        System.out.println(length+"---"+adminCount);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(admins)+"-"+length;
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    private boolean isEmail(String email) {
        return true;
    }
    private boolean isUsername(String username) {
        username.replace(" ", "");
        System.out.println(username);
        if(username.length() >= 6 && username.length() <= 20)
            return true;
        return false;
    }
    private boolean isPassword(String password) {
        password.replace(" ", "");
        if(password.length() >= 6 && password.length() <= 20)
            return true;
        return false;
    }
}